import requests
import json
import cv2
from requests.api import get
import matplotlib.patches as patches
import matplotlib.pyplot as plt
import base64
import tkinter as tk
from tkinter import filedialog

app_id='cfce8fae8a514825ae6c48464fdf8e00'
app_key='35bcc595c35a4590978197c3ae8f4a35'
app_url='http://api.eyekey.com/'

#把face加入people
def Create_People(people_name,face_id):
    Url = app_url+'People/people_create?app_id='+app_id+'&'+'app_key='+app_key+'&'+'face_id='+face_id+'&'+'people_name='+people_name
    res = requests.get(url=Url)
    print('已加入'+people_name)
    print(res.text)

def getPeople(people_name):
    Url=app_url+'People/people_get?app_id='+app_id+'&'+'app_key='+app_key+'&'+'people_name='+people_name
    res = requests.get(url=Url)
    print(res.text) 

#对比的方法
def Compare(face_id_1,face_id_2):
    Url = app_url+'face/Match/match_compare?app_id='+app_id+'&'+'app_key='+app_key+'&'+'face_id1='+face_1+'&'+'face_id2='+face_2
    print(Url)
    res = requests.get(url=Url)
    #JSON_res = json.loads(res.text)
    print(res.text)

#下载网图用的方法
def Down_load(URL):
    response = requests.post(URL)
    if response.status_code == 200:
        open('C:\\Users\\15203\\Desktop\\python\\IMG\\Test.png','wb').write(response.content)
        print('done')
#

#图片的URL
#URL_image = 'http://img.pconline.com.cn/images/upload/upc/tx/photoblog/1509/09/c3/12374162_1441765474392.jpg'
#下载照片
#Down_load(URL_image)
#本地图片上传检测
def select_picture():
    root = tk.Tk()
    root.withdraw()
    Fpath = filedialog.askopenfilename()
    dir=Fpath
    basef=open(dir.split('.')[0]+'_base64.txt','w') 
    with open (dir,'rb') as f:
        base64_data=base64.b64encode(f.read())
        s=base64_data.decode()
        data='%s'%s
        # print(data)
        basef.write(data)
    basef.close()
    #请求的URL
    URL_request=app_url+'face/Check/checking'
    #核对组装起来的URL
    #print(URL_request)
    res = requests.post(url=URL_request,data={
        'app_id':app_id,
        'app_key':app_key,
        'img':data
        })
    print(res.text)
    JSON_res = json.loads(res.text)
    face_id = JSON_res['face'][0]['face_id']
    print('face_id:'+face_id)
    return face_id

face_1 = select_picture()
face_2 = select_picture()

Compare(face_1,face_2)

Create_People('王永超',face_2)

getPeople('王永超')
#解析json
# JSON_res = json.loads(res.text)

# #测试json对象
# face = JSON_res['face']
# #中心点坐标
# print(face[0]['position']['center']['x'])
# print(face[0]['position']['center']['y'])
# #脸的高和宽
# print(face[0]['position']['height'])
# print(face[0]['position']['width'])


    
